package com.game;


import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.opengl.Matrix;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;


public class GameActivity extends Activity {
    /** Called when the activity is first created. */
	private GraphicsScreen Screen;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
    	///// This is called when the app is started.
        super.onCreate(savedInstanceState); // Not a clue! Must find out!
        Screen = new GraphicsScreen(this); // starts up the GraphicsScreen class below
 	    setContentView(Screen); // This sets what the screen should set as the default view
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // Locks the screen Rotation to Landscape
        new Thread(new Runnable() {
		    public void run() {
		    	
		     while(true){
		    	
		    	

				try {
					GameLevel.updateLevel();
					Thread.sleep(50);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
		    	 
		     }
		    }
		  }).start();

    }
    @Override
    public void onPause(){
    	super.onPause();
    	
    	/// !!!  Call GameSave functions here and shizzle ///
    	
    	didpause = true; // just a test!
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	//levelScrollX = (int)event.getX();
    	
        	
    	if(event.getAction() == MotionEvent.ACTION_DOWN){
    		
    		if(event.getX() > 600){
    			GameLevel.scrollRight = true;
    		}
    		else if(event.getX() < 100){
    			GameLevel.scrollLeft = true;
    		}
    	}
    	if(event.getAction() == MotionEvent.ACTION_UP){
    		//levelScrollX = (int)event.getX();
    		
    		GameLevel.scrollRight = false;
    		GameLevel.scrollLeft = false;
    	}
    	
    	
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            //case MotionEvent.ACTION_MOVE:
            //case MotionEvent.ACTION_UP:
            	
        }
    return false;
    }
    
    static int levelScrollX = 0;
   
    static boolean didpause = false;
}

class GraphicsScreen extends View{
	Bitmap playerBase,background;
	public GraphicsScreen(Context context) { // This class handles all rendering and has its own thread loop to set the fps
		super(context);
		playerBase = BitmapFactory.decodeResource(getResources(), R.drawable.castle);
		background = BitmapFactory.decodeResource(getResources(), R.drawable.background);
		
		new Thread(new Runnable() {
		    public void run() {
		    	
		     while(true){
		    	
		    	

				postInvalidate();
		    	 
		     }
		    }
		  }).start();

		// TODO Auto-generated constructor stub
	}
	protected void onDraw(Canvas canvas) {
	    

	    Paint paint = new Paint();
	    paint.setColor(Color.BLACK);
	    
	    canvas.drawRect(0,0,getWidth(),getHeight(), paint);
	    canvas.drawBitmap(background, (int) (0 + GameActivity.levelScrollX*0.1),0,paint);
	    canvas.drawBitmap(background, (int) (-600 + GameActivity.levelScrollX*0.1),0,paint);
	    canvas.drawBitmap(background, (int) (600 + GameActivity.levelScrollX*0.1),0,paint);

	    canvas.drawBitmap(playerBase, 0 + GameActivity.levelScrollX,(int) (getHeight()*0.6),paint);
	   

	    
	    paint.setColor(Color.YELLOW);
	    canvas.drawText("Scroll == " + GameActivity.levelScrollX + "   Pause? " + GameActivity.didpause, 20, 20, paint);
	   
	  }
}
